package org.jeecg.common.lib;

import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.TreeMap;

@Slf4j
public class SybPayService {
	/**
	 * 扫码预消费
	 * @param trxamt 金额
	 * @param reqsn 商户订单号
	 * @param paytype 预消费APP类型（微信/支付宝）
	 * @param body  
	 * @param remark
	 * @param validtime  有效时间
	 * @param authcode 被扫付款码
	 * @param acct    微信Openid，支付宝userid
	 * @param notify_url  异步交易回调地址
	 * @return
	 * @throws Exception 
	 */
	public Map<String,String> preScanPay(String trxamt,String reqsn,String paytype,String body,
			String remark,String validtime,String authcode,String acct,String notify_url) throws Exception{
		HttpConnectionUtil http = new HttpConnectionUtil(SybConstants.SYB_PREAPIURL+"/pay");
		http.init();
		TreeMap<String,String> params = new TreeMap<String,String>();
		if(!SybUtil.isEmpty(SybConstants.SYB_ORGID))
			params.put("orgid", SybConstants.SYB_ORGID);
		params.put("cusid", SybConstants.SYB_CUSID);
		params.put("appid", SybConstants.SYB_APPID);
		params.put("version", SybConstants.VERSION);
		params.put("trxamt", trxamt);
		params.put("reqsn",  reqsn);
		params.put("paytype", paytype);
		params.put("randomstr", SybUtil.getValidatecode(8));
		params.put("body", body);
		params.put("remark", remark);
		params.put("validtime", validtime);
		params.put("authcode", authcode);
		
		params.put("acct", acct);//微信公众号支付为OPENID，支付宝支付-用户user_id
		params.put("notify_url", notify_url);
		params.put("signtype", SybConstants.SIGN_TYPE);
		String appkey = "";
		if(SybConstants.SIGN_TYPE.equals("RSA"))
			appkey = SybConstants.SYB_RSACUSPRIKEY;
		else if(SybConstants.SIGN_TYPE.equals("SM2"))
			appkey = SybConstants.SYB_SM2PPRIVATEKEY;
		else 
			appkey = SybConstants.SYB_MD5_APPKEY;
		params.put("sign", SybUtil.unionSign(params,appkey,SybConstants.SIGN_TYPE));
		byte[] bys = http.postParams(params, true);
		String result = new String(bys,"UTF-8");
		Map<String,String> map = handleResult(result);
		return map;
	}
	/**
	 * 扫码预消费撤销
	 * @param trxamt  撤销金额
	 * @param reqsn   第三方撤销订单号
	 * @param oldtrxid通联支付原预消费交易单号
	 * @return
	 * @throws Exception
	 */

	public Map<String,String> preCancel(String trxamt,String reqsn,String oldtrxid) throws Exception{
		HttpConnectionUtil http = new HttpConnectionUtil(SybConstants.SYB_PREAPIURL+"/cancel");
		http.init();
		TreeMap<String,String> params = new TreeMap<String,String>();
		if(!SybUtil.isEmpty(SybConstants.SYB_ORGID))
			params.put("orgid", SybConstants.SYB_ORGID);
		params.put("cusid", SybConstants.SYB_CUSID);
		params.put("appid", SybConstants.SYB_APPID);
		params.put("version", SybConstants.VERSION);
		params.put("trxamt", trxamt);
		params.put("reqsn", reqsn);
		params.put("oldtrxid", oldtrxid);//
		params.put("randomstr", SybUtil.getValidatecode(8));
		params.put("signtype", SybConstants.SIGN_TYPE);
		String appkey = "";
		if(SybConstants.SIGN_TYPE.equals("RSA"))
			appkey = SybConstants.SYB_RSACUSPRIKEY;
		else if(SybConstants.SIGN_TYPE.equals("SM2"))
			appkey = SybConstants.SYB_SM2PPRIVATEKEY;
		else 
			appkey = SybConstants.SYB_MD5_APPKEY;
		params.put("sign", SybUtil.unionSign(params,appkey,SybConstants.SIGN_TYPE));

		log.info("通联请求地址:"+http.getConnectUrl());
		log.info("向通联携带请求参数:"+params);

		byte[] bys = http.postParams(params, true);
		String result = new String(bys,"UTF-8");
		Map<String,String> map = handleResult(result);

		return map;
	}
	
	/**
	 * 预消费完成
	 * @param oldtrxid  通联支付原预消费交易单号
	 * @param reqsn     第三方完成订单号
	 * @param trxamt    完成金额
	 * @return
	 * @throws Exception
	 */

	public Map<String,String> preFinish(String oldtrxid,String reqsn,String trxamt) throws Exception{
		HttpConnectionUtil http = new HttpConnectionUtil(SybConstants.SYB_PREAPIURL+"/finish");
		http.init();
		TreeMap<String,String> params = new TreeMap<String,String>();
		if(!SybUtil.isEmpty(SybConstants.SYB_ORGID))
			params.put("orgid", SybConstants.SYB_ORGID);
		params.put("cusid", SybConstants.SYB_CUSID);
		params.put("appid", SybConstants.SYB_APPID);
		params.put("version", SybConstants.VERSION);
		params.put("oldtrxid", oldtrxid);
		params.put("reqsn", reqsn);
		params.put("trxamt", trxamt);
		params.put("randomstr", SybUtil.getValidatecode(8));
		params.put("signtype", SybConstants.SIGN_TYPE);
		String appkey = "";
		if(SybConstants.SIGN_TYPE.equals("RSA"))
			appkey = SybConstants.SYB_RSACUSPRIKEY;
		else if(SybConstants.SIGN_TYPE.equals("SM2"))
			appkey = SybConstants.SYB_SM2PPRIVATEKEY;
		else 
			appkey = SybConstants.SYB_MD5_APPKEY;
		params.put("sign", SybUtil.unionSign(params,appkey,SybConstants.SIGN_TYPE));

		log.info("通联请求地址:"+http.getConnectUrl());
		log.info("向通联携带请求参数:"+params);

		byte[] bys = http.postParams(params, true);
		String result = new String(bys,"UTF-8");
		Map<String,String> map = handleResult(result);

		return map;
	}
	/**
	 * 扫码预消费完成退款
	 * @param trxamt   退款金额
	 * @param reqsn    第三方退款订单号 
	 * @param oldtrxid 通联支付原预消费完成交易单号
	 * @param remark   备注
	 * @return
	 * @throws Exception
	 */
	public Map<String,String> preRefund( String trxamt,String reqsn,String oldtrxid,String remark) throws Exception{
		HttpConnectionUtil http = new HttpConnectionUtil(SybConstants.SYB_PREAPIURL+"/refund");
		http.init();
		TreeMap<String,String> params = new TreeMap<String,String>();
		if(!SybUtil.isEmpty(SybConstants.SYB_ORGID))
			params.put("orgid", SybConstants.SYB_ORGID);
		params.put("cusid", SybConstants.SYB_CUSID);
		params.put("appid", SybConstants.SYB_APPID);
		params.put("version", SybConstants.VERSION);
		params.put("trxamt", trxamt);
		params.put("reqsn", reqsn);
		params.put("oldtrxid", oldtrxid);
		params.put("remark", remark);
		params.put("randomstr", SybUtil.getValidatecode(8));
		params.put("signtype", SybConstants.SIGN_TYPE);
		String appkey = "";
		if(SybConstants.SIGN_TYPE.equals("RSA"))
			appkey = SybConstants.SYB_RSACUSPRIKEY;
		else if(SybConstants.SIGN_TYPE.equals("SM2"))
			appkey = SybConstants.SYB_SM2PPRIVATEKEY;
		else 
			appkey = SybConstants.SYB_MD5_APPKEY;
		params.put("sign", SybUtil.unionSign(params,appkey,SybConstants.SIGN_TYPE));
		byte[] bys = http.postParams(params, true);
		String result = new String(bys,"UTF-8");
		Map<String,String> map = handleResult(result);
		return map;
	}
	/**
	 *  扫码预消费各类交易的查询
	 * @param trxid   通联支付预消费各类交易单号
	 * @param reqsn   第三方订单号 
	 * @return
	 * @throws Exception
	 */



	public Map<String,String> preQuery(String trxid,String reqsn) throws Exception{
		HttpConnectionUtil http = new HttpConnectionUtil(SybConstants.SYB_PREAPIURL+"/query");
		http.init();
		TreeMap<String,String> params = new TreeMap<String,String>();
		params.put("cusid", SybConstants.SYB_CUSID);
		params.put("appid", SybConstants.SYB_APPID);
		params.put("trxid", trxid);
		//params.put("reqsn", reqsn);
		params.put("randomstr", SybUtil.getValidatecode(8));
		params.put("signtype", SybConstants.SIGN_TYPE);
		String appkey = "";
		appkey = SybConstants.SYB_RSACUSPRIKEY;
		params.put("sign", SybUtil.unionSign(params,appkey,SybConstants.SIGN_TYPE));

		log.info("通联请求地址:"+http.getConnectUrl());
		log.info("向通联携带请求参数:"+params);

		byte[] bys = http.postParams(params, true);
		String result = new String(bys,"UTF-8");
		Map<String,String> map = handleResult(result);


		return map;
	}
	
	
	
	
	
	
	
	public static Map<String,String> handleResult(String result) throws Exception{
		System.out.println("data:"+result);
		Map map = SybUtil.json2Obj(result, Map.class);
		log.info("通联返回参数:"+map);
		if(map == null){
			throw new Exception("返回数据错误");
		}
		if("SUCCESS".equals(map.get("retcode"))){
			TreeMap tmap = new TreeMap();
			tmap.putAll(map);
			String appkey = "";
			if(SybConstants.SIGN_TYPE.equals("RSA"))
				appkey = SybConstants.SYB_RSATLPUBKEY;
			else if(SybConstants.SIGN_TYPE.equals("SM2"))
				appkey = SybConstants.SYB_SM2TLPUBKEY;
			else 
				appkey = SybConstants.SYB_MD5_APPKEY;
			if(SybUtil.validSign(tmap, appkey, SybConstants.SIGN_TYPE)){
				System.out.println("签名成功");
				return map;
			}else{
				throw new Exception("验证签名失败");
			}
			
		}else{
			throw new Exception(map.get("retmsg").toString());
		}
	}
	
	
}
